/**
 * Copyright (C) 2008+ Spark Zheng
 *
 * test/test_misc.c
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "misc.h"
#include "global.h"

void test_rr_scheduler(void);
void test_rr_scheduler()
{
	int i, j;

	i = 1000;
	j = 81;

	fprintf(stderr, "sqrt %d is %f, sqrt %d is %f.\n",
		i, quake_sqrt((float)i), j, quake_sqrt((float)j));
	i = 1000;
	fprintf(stderr, "near %d primer is %d.\n", i , near_primer(i));

	uint32_t ii = 4000000000UL;
	fprintf(stderr, "near %u primer is %u.\n", ii , near_primer(ii));

	int i_min = 0, i_max = 0;

	/* test for rr_scheduler */
	uint32_t weight[5] = { 2, 1, 6, 8, 3 };
	int value[5] = { 1, 2, 3, 4, 5 };

	i = j = 5;
	i_min = min_n(value, &i);
	i_max = max_n(value, &j);
	fprintf(stderr, "use min_n: min_v is %d[%d], use max_n: max_v is %d[%d]\n",
		i_min, i, i_max, j);
	i = j = 3;
	i_min = dlib_nmin(&i, value[0], value[2], value[4]);
	i_max = dlib_nmax(&j, value[0], value[2], value[4]);
	fprintf(stderr, "use nmin: min_v is %d[%d], use nmax: max_v is %d[%d]\n",
		i_min, i, i_max, j);
	fprintf(stderr, "gcd_n is %u, lcm_n is %u\n", gcd_n(weight, 5),
		lcm_n(weight, 5));

	uint32_t prev = 0;
	int cw = 0;
	for (i = 0; i < 20; i++) {
		prev = rr_scheduler(weight, 5, prev, &cw);
		fprintf(stderr, "id[%d]: %d\n", i, value[prev]);
	}

	return;
}

void test_dp(const char *str_a, const char *str_b);
void test_dp(const char *str_a, const char *str_b)
{
	size_t len_a = strlen(str_a);
	size_t len_b = strlen(str_b);
	size_t len = dlib_max(len_a, len_b);
	char ret[len];
	ret[len-1] = '\0';

	lcs_dp(str_a, len_a, str_b, len_b, ret, &len);
	ret[len] = '\0';
	fprintf(stderr, "LCS [%s] and [%s] is: [%s][%d]\n",
		str_a, str_b, ret, len);

	memset(ret, 0, len);

	lis_dp(str_b, len_b, ret, &len);
	ret[len] = '\0';
	fprintf(stderr, "LIS [%s] is: [%s][%d]\n", str_b, ret, len);

	int32_t ssm_len = ssm_dp(str_a, len_a, str_b, len_b); 
	fprintf(stderr, "SSM [%s] and [%s] is: [%d]\n", str_a, str_b, ssm_len);

	int32_t acs_len = acs_dp(str_a, len_a, str_b, len_b); 
	fprintf(stderr, "ACS [%s] and [%s] is: [%d]\n", str_a, str_b, acs_len);

	len = dlib_max(len_a, len_b);
	memset(ret, 0, len);
	int32_t ld_len = levenshtein(str_a, len_a, str_b, len_b, ret);
	ret[len] = '\0';
	fprintf(stderr, "LD [%s] and [%s] is: [%s][%d]\n", str_a, str_b, ret, ld_len);


	float similarity = 0;
	similarity = str_similar_lcs(str_a, len_a, str_b, len_b);
	fprintf(stderr, "S-LCS [%s] and [%s] is: [%f]\n", str_a, str_b, similarity);
	similarity = 0;
	similarity = str_similar_ld(str_a, len_a, str_b, len_b);
	fprintf(stderr, "S-LD [%s] and [%s] is: [%f]\n", str_a, str_b, similarity);


	return;
}

int main(int argc, char *argv[])
{
	test_rr_scheduler();

	//char *str_a = "10101011010011001011010101";
	char *str_a = "1110101100";
	char *str_b = "1010101010";

	if (argc > 1)
		str_a = argv[1];
	if (argc > 2)
		str_b = argv[2];

	test_dp(str_a, str_b);

	return 0;
}
